<?php

/**
 * A view that only generates output 
 * This view is/can be used from several controllers
 */

class LoginView {
  	
	//Constants to obtain values from login form	
	const username = "Username";
	const password = "Password";
	const remember = "remember";
	  
	const loginButton = "Login";
	const logoutButton = "Logout";
	
	private $cookieExpires;
	private $userToLogin = "";
  
	/**
	 * Constructor, sets the expires@time for cookies
	 */
	public function __construct(){
  		$this->cookieExpires = time() + 60 * 3;
		if(isset($_GET[NavigationView::username])){
			$this->userToLogin = $_GET[NavigationView::username];
			unset($_GET[NavigationView::username]);
		}
	}
	
  /**
   * Takes status as arguments, 0: no errors, 1: Wrong credentials, 2: ...
   * Effects outcome message
   * @access public
   * @return XHTML - Div containing login boxes and submitbutton
   * @param int status
   */  
  public function generateLoginBox($bool) {
    
	//If true, all's good...
	if($bool){
		$message = "";	
	}
	//If false wrong cred was given
	else {
		$message = "<h2 class='fileErrorP'>Wrong credentials!</h2>";
	}	
	
    return "<div class='loginDiv'>
    	<form action='' method='post'>
			$message
    		  <fieldset>			
			    <legend><strong>Enter Username and Password:</strong></legend>
			    
			    <label for='Username'>Username:</label>
			    <input type='text' size='30' id='Username' name='".LoginView::username."' value='$this->userToLogin'/><br />
			    
			    <label for='PassWord'>Password:</label>
			    <input type='password' size='30' id='Password' name='".LoginView::password."'/><br />
			    
			    <input type='checkbox' name='".LoginView::remember."' class='textbox' text='Remenber me'>Remember me!</input><br />
			    
			    <input type='Submit' name='".LoginView::loginButton."' value='Login' />
			  </fieldset>
		</form>
    </div>";
  }
  
  /**
   * Function to generate Logout button
   * @access public
   * @return submitbutton -> logout
   * @param -
   */
	public function generateLogoutButton(){
  		return "<div class='loginDiv'>
  					<h2>Your in pall!</h2>
  					<form action='?". NavigationView::controller . "=" . NavigationView::login ."' method='post'>
  						<input class='logout' type='Submit' name='".LoginView::logoutButton."' value='Logout' />
					</form>
					<div class='clear'></div>
				</div>";	
	}
  
	/**
	 * Returns username if stored in cookie or $_GET
	 * @access public
	 * @param -
	 * @return string
	 */
	public function getUserName(){
	  	//Check if username has a value	(get or cookie)
	  	//it it does, return it
		if (isset( $_POST[LoginView::username]) == true) {
	  		return $_POST[LoginView::username];
	    } 
		elseif(isset($_COOKIE[LoginView::username]) == true){
	  		return $_COOKIE[LoginView::username];
	  	}
	    //else return false
	    return FALSE;	
	}
  
	/**
	 * Returns password if stored in cookie or $_GET
	 * @access public
	 * @param -
	 * @return string
	 */
	public function getPassword(){
	  	//Check if password has a value	(get or cookie)
	  	//it it does, return it
		if (isset( $_POST[LoginView::password]) == true) {
	      return $_POST[LoginView::password];
	    } 
		elseif(isset($_COOKIE[LoginView::password]) == true){
			return$_COOKIE[LoginView::password];
		}
		//else return false
    	return FALSE; 	
	}
	
	
	/**
	 * Returns a UserObject as $user
	 * @access public
	 * @param -
	 * @return string
	 */
	public function getUserCredentials(){
		//Check if password has a value	(get or cookie)
	  	//it it does, return it
	  	if(!InputValidationHandler::isValidPass($_POST[LoginView::password])){
	  		return $user = User::create(0, "", "");
	  	}
		if (isset($_POST[LoginView::username]) == true) {
	      $user = User::create(0, $_POST[LoginView::username], $_POST[LoginView::password]);	
	      return $user;
	    } 
		elseif(isset($_COOKIE[LoginView::username]) == true){
			$user = User::create(0, $_COOKIE[LoginView::username], $_COOKIE[LoginView::password]);	
	      	return $user;
		}
		//else return false
    	return $user = User::create(0, "", "");
	}

	/**
	 * Returns true or false indicating if user wants to be remembered
	 * @access public
	 * @param -
	 * @return Boolean	
	 */
    public function getRememberStatus(){
	  	//Check if password has a value	
	  	//it it does, return true
	  	if (isset( $_POST[LoginView::remember]) == true) {
	      return true;
	    } 
		//else return false
	    return FALSE; 	
	}
	
	/**
	 * Checks if user wants to be remembered, if true, creates cookie with username and password
	 * @access public
	 * @param UserObject
	 * @return Boolean
	 */
	public function setCookie($user){
		if($this->getRememberStatus() == true){
			setcookie(LoginView::username, $user->UserName, $this->cookieExpires);
			setcookie(LoginView::password, $user->PassWord, $this->cookieExpires);
			return true;
		}
		return false;
	}
	
	/**
	 * Clear cookie by setting expiredate to a negative
	 * @access public
	 * @param -
	 * @return -
	 */
	public function clearCookie(){
		setcookie (LoginView::username, "", time()- 3600);
		setcookie (LoginView::password, "", time()- 3600);
		unset($_COOKIE[LoginView::username]);
		unset($_COOKIE[LoginView::password]);
	}
    
	/**
	 * checks if user pressed login button 
	 * OR if we have a cookie stored with username and pass
	 * @access public
	 * @param -
	 * @return boolean
	 */
	public function triedToLogIn(){
	  	//Check if user tried to login, by click or cookie
	  	//If it has, return true
	  	if (isset($_POST[LoginView::loginButton]) == true){	
	  		return true;
	  	}
		elseif(isset($_COOKIE[LoginView::username]) == true){
			return true;
		}
	  	//If not, return false
	  	return false;
	}
  
	/**
	 * Checks if pressed logout button
	 * @access public
	 * @param -
	 * @return Boolean
	 */
	public function triedToLogOut(){
	  	//Check if Logout button has been clicked
	  	//If it has, return true
		if(isset($_GET[NavigationView::logOutAction]) == true){	
			return true;
		}
		//If not, return false
		return false;
	}
}